﻿using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace Mars.MeshUI
{
    [CustomEditor(typeof(MeshCanvas))]
    public class MeshCanvasEditor : Editor
    {
        static int fateCount = 0;
        static Transform targetTransform;

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            MeshCanvas cfg = target as MeshCanvas;

            if (GUILayout.Button("Baker"))
            {
                if (Application.isPlaying == false)
                    BackerUI(cfg);
            }

            if (GUILayout.Button("Clear Ugui"))
            {
                ClearUgui(cfg.transform);
            }
        }

        void ClearUgui(Transform root)
        {
            var effects = root.GetComponentsInChildren<BaseMeshEffect>(true);

            foreach (var effect in effects)
            {
                DestroyImmediate(effect);
            }

            var g = root.GetComponentsInChildren<Graphic>(true);

            foreach (var graphic in g)
            {
                DestroyImmediate(graphic);
            }

            var canvasRender = root.GetComponentsInChildren<CanvasRenderer>(true);

            foreach (var canvas in canvasRender)
            {
                DestroyImmediate(canvas);
            }
        }

        public static void BackerUI(MeshCanvas mesRender)
        {
            if (mesRender != null)
            {
                Backer(mesRender);
            }
        }

        static void Backer(MeshCanvas mesRender)
        {
            Transform transform = mesRender.transform;

            var g = transform.GetComponentsInChildren<Img>(true);

            foreach (var img in g)
            {
                if (img.gameObject.activeSelf == false)
                {
                    img.gameObject.SetActive(true);
                }

                if (img.enabled == false)
                {
                    img.enabled = true;
                }

                var meshImage = img.GetComponent<MeshImg>();

                if (meshImage == null)
                {
                    img.gameObject.AddComponent<MeshImg>();
                }

                var baker = img.GetComponent<MeshBakerWidget>();

                if (baker == null)
                {
                    img.gameObject.AddComponent<MeshBakerWidget>();
                }

                img.SetAllDirty();
            }


            var t = transform.GetComponentsInChildren<Text>(true);

            foreach (var text in t)
            {
                var meshText = text.GetComponent<MeshText>();

                if (meshText == null)
                {
                    meshText = text.gameObject.AddComponent<MeshText>();
                }

                meshText.SetSetting(text.GetGenerationSettings(text.rectTransform.rect.size));
                meshText.text = text.text;
                meshText.meshFont = text.font;
                meshText.pivot = text.rectTransform.pivot;
                text.enabled = false;
            }


            targetTransform = transform;

            EditorApplication.update += UpdateCallBack;
        }

        static void UpdateCallBack()
        {
            if (targetTransform == null)
            {
                CancelUpdate();
                return;
            }

            if (fateCount < 50)
            {
                fateCount++;
                return;
            }

            var render = targetTransform.GetComponent<MeshCanvas>();

            var bakers = targetTransform.GetComponentsInChildren<MeshBakerWidget>();

            for (int i = 0; i < bakers.Length; i++)
            {
                Graphic graphic = bakers[i].GetComponent<Graphic>();
                DestroyImmediate(bakers[i]);

                if (graphic != null)
                {
                    graphic.enabled = false;
                }
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            CancelUpdate();
        }

        static void CancelUpdate()
        {
            targetTransform = null;
            fateCount = 0;
            EditorApplication.update -= UpdateCallBack;
        }
    }
}